﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using MvcApp.CommonSupport.Unity;
using System.Configuration;

namespace MvcApp
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
        }

        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                "LogOn", // 路由名称
                "LogOn", // 带有参数的 URL
                new { controller = "Home", action = "LogOn" } // 参数默认值
            );

            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Home", action = "LogOn", id = UrlParameter.Optional } // Parameter defaults
            );

        }

        protected void Application_Start()
        {
            DependencyResolver.SetResolver(new UnityDependencyResolver());
            
            AreaRegistration.RegisterAllAreas();

            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);
        }

        //public void ExceptionHandlerStarter()
        //{
        //    String exMode = ConfigurationManager.AppSettings["ExceptionMode"];
        //    if (!String.IsNullOrEmpty(exMode) && string.Compare(exMode.ToLower(), "on") == 0)
        //    {
        //        string s = HttpContext.Current.Request.Url.ToString();
        //        HttpServerUtility server = HttpContext.Current.Server;
        //        if (server.GetLastError() != null)
        //        {
        //            Exception lastError = server.GetLastError();
        //            Application["LastError"] = lastError;
        //            int statusCode = HttpContext.Current.Response.StatusCode;
        //            string exceptionOperator = ConfigurationManager.AppSettings["ExceptionUrl"];
        //            try
        //            {
        //                if (!String.IsNullOrEmpty(exceptionOperator))
        //                {
        //                    exceptionOperator = new System.Web.UI.Control().ResolveUrl(exceptionOperator);
        //                    string url = string.Format("{0}?ErrorUrl={1}", exceptionOperator, server.UrlEncode(s));
        //                    string script = String.Format("<script language='javascript' type='text/javascript'>window.top.location='{0}';</script>", url);
        //                    Response.Write(script);
        //                    Response.End();
        //                }
        //            }
        //            catch (Exception)
        //            {
        //            }
        //        }
        //    }
        //}
        ///// <summary>
        ///// 错误处理
        ///// </summary>
        ///// <param name="sender"></param>
        ///// <param name="e"></param>
        //protected void Application_Error(object sender, EventArgs e)
        //{
        //    ExceptionHandlerStarter();
        //}
    }
}